package com.cs.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cs.domain.entity.User;
import com.cs.domain.entity.UserInfo;
import com.cs.store.dao.IGoodsType;
import com.cs.store.dao.IStoreDao;
import com.cs.store.service.IStoreService;
import com.cs.domain.entity.GoodsType;
import com.cs.domain.entity.Store;
import com.cs.domain.vo.StoreGoodsVo;
import com.cs.store.dao.IStoreGoodsVoDao;
import com.cs.user.dao.IUserDao;
import com.cs.user.dao.IUserInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StoreServiceImpl implements IStoreService {
    @Autowired
    private IStoreDao iStoreDao;
    @Autowired
    @Lazy
    private IStoreGoodsVoDao iStoreGoodsVoDao;
    @Autowired
    private IGoodsType iGoodsType;

    @Autowired
    private IUserInfoDao infoDao;

    @Autowired
    private IUserDao iUserDao;


    @Override
    public List<Store> findAllStore() {
        return iStoreDao.selectList(null);
    }

    @Override
    public List<GoodsType> findAllType() {
        return iGoodsType.selectList(null);
    }


    @Override
    public List<StoreGoodsVo> findGoodsBySt(String storename, String goodsType) {
        QueryWrapper wrapper = new QueryWrapper<>();
        if (storename != null && storename != "") {
            wrapper.like("store_name", storename);
        }
        if (goodsType != null && goodsType != "") {
            wrapper.like("type_name", goodsType);
        }
        ;
        return iStoreGoodsVoDao.selectList(wrapper);
    }
    @Override
    public Map findStoreInfo(String username) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getUsername,username);
        List<UserInfo> list = infoDao.selectList(wrapper);
        UserInfo user = new UserInfo();
        Store store = new Store();
        if(list.size()!=0){
            user.setUsername(list.get(0).getUsername());
            user.setEmail(list.get(0).getEmail());
            Long storeUserId = list.get(0).getId();
            LambdaQueryWrapper<Store> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(Store::getStoreUserId,storeUserId);
            Store s = iStoreDao.selectOne(wrapper2);
            store.setStoreName(s.getStoreName());
            store.setStoreAddress(s.getStoreAddress());

            Map<String,Object> map =new HashMap<>();
            map.put("user",user);
            map.put("store",store);


            return map;
        }else{
            return null;
        }
    }

    @Override
    public Store findTheStoreId(String storeUserName) {
        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        Wrapper.eq("user_username", storeUserName);
        Long userId = iUserDao.selectOne(Wrapper).getId();
        QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_user_id", userId);
        Store store = iStoreDao.selectOne(queryWrapper);
        return store;
    }

}
